package com.alibaba.citrus.cr.forecast.functions.model;

import com.alibaba.cz.base.tool.log.Log;
import com.epoch.app.crforecastsales.sdo.ForecastOrderLineSDO;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import com.epoch.app.crplatformenhance.model.dto.StrategyPageQueryResponse;
import com.google.common.collect.Maps;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;

public class AnalyzeProcessDO {
    public static final String PREFIX_LOG_SYS_ERROR = "| ANALYZE | SYS_ERROR | ";
    public static final String PREFIX_LOG_BIZ = "| ANALYZE | BIZ |";
    private final Log log = Log.getLogger(AnalyzeProcessDO.class);

    @Getter
    //主单号与主单信息map
    private Map<String, ForecastOrderSDO> forecastOrderMap;
    @Getter
    private List<ForecastOrderLineSDO> orderLineListToAnalyze;
    @Getter
    private List<ForecastOrderLineSDO> orderLineListAnalyzedDone;
    @Getter
    private String batchNo;
    //用于打印业务日志，记录业务检查节点信息
    @Getter
    @Setter
    private String lastMessage;
    @Getter
    @Setter
    private String lastError;
    //箱规转换开关
    @Getter
    @Setter
    private Boolean cartonSwitch;
    //运输规格
    @Getter
    @Setter
    private Integer transportUnit;

    //增加暂存模型，减少N次strategyQueryService.pageQuery查询（N==主单个数），不太优雅，降低代码改造风险先这么改了
    @Getter
    @Setter
    private Map</* orderId */ String, List<StrategyPageQueryResponse>> orderStrategyPageQueryResponseMap;

    public AnalyzeProcessDO(String batchNumber, List<ForecastOrderLineSDO> toAnalyzeList) {
        batchNo = batchNumber;
        forecastOrderMap = new HashMap<>();
        orderLineListAnalyzedDone = new ArrayList<>();
        orderLineListToAnalyze = new ArrayList<>(toAnalyzeList);
        orderStrategyPageQueryResponseMap = Maps.newHashMap();
        cartonSwitch=null;
        transportUnit=null;
        lastMessage = "";
        lastError = "";
    }

    public int getFinishCount() {
        return orderLineListAnalyzedDone.size();
    }

    public boolean finishAnalyze(ForecastOrderLineSDO orderLine) {
        if(orderLineListToAnalyze.remove(orderLine)){
            orderLineListAnalyzedDone.add(orderLine);
        }
        return true;
    }

    public void setTodoList(List<ForecastOrderLineSDO> orderLines){
        orderLineListToAnalyze=orderLines;
    }

    public void setToDoneList(List<ForecastOrderLineSDO> orderLines){
        orderLineListAnalyzedDone=orderLines;
    }

    /**
     * 起运量检查节点的日志
     * 起运量节点：批次号、子单号、标准值（策略id、组织A、维度类型、维度名称、起运量100）、
     * 检查值（仓库、货品、预报量累计和、同仓同货子单列表）、结果值（结果、原因）
     */
    public void bizAnalyzeLogWithCheckShipmentVolume(String msg, List<ForecastOrderLineSDO> orderLineList) {
        if (CollectionUtils.isEmpty(orderLineList)) {
            return;
        }
        List<String> listIds = new ArrayList<>();
        for (ForecastOrderLineSDO orderLine : orderLineList) {
            listIds.add(orderLine.getId().toString());
        }

        log.error(PREFIX_LOG_BIZ + "| 批次号: " + getBatchNo() + "| Step: 起运量检查 |子单id列表: " + listIds + getLastMessage() + "|检查结果: " + msg);
    }

    /**
     * 库存分解检查节点的日志
     */
    public void bizAnalyzeLogWithDistributeInventory(List<ForecastOrderLineSDO> orderLineList) {
        if (CollectionUtils.isEmpty(orderLineList)) {
            log.error(PREFIX_LOG_BIZ + "| 批次号: " + getBatchNo() + "| Step: 库存分配 | 子单id列表: " + getLastMessage());
            return;
        }
        List<String> listIds = new ArrayList<>();
        for (ForecastOrderLineSDO orderLine : orderLineList) {
            listIds.add(orderLine.getId().toString());
        }

        log.error(PREFIX_LOG_BIZ + "| 批次号: " + getBatchNo() + "| Step: 库存分配 | 子单id列表: " + listIds + getLastMessage());

    }

    /**
     * 库存分解检查节点的日志
     */
    public void bizAnalyzeLogWithDistributeInventory(ForecastOrderLineSDO orderLine, String resultMessage) {
        if (Objects.isNull(orderLine)) {
            return;
        }
        log.error(PREFIX_LOG_BIZ + "| 批次号: " + getBatchNo() + "| Step: 库存分配 | 子单id列表: " + orderLine.getId() + getLastMessage() +
                "| 检查结果: " + resultMessage);

    }

    /**
     * 月度计划检查节点的日志
     */
    private void bizAnalyzeLogWithCheckMonthPlan() {

    }

    /**
     * 信用检查节点的日志
     */
    private void bizAnalyzeLogWithCheckCredit() {

    }

    /**
     * 活动检查节点的日志
     */
    private void bizAnalyzeLogWithCheckActivity() {

    }

    /**
     * 业务日志记录
     */
    private void bizErrorLog(String msg) {
        log.error("ERROR" + msg);
    }

    /**
     * 业务日志记录
     */
    public void bizAnalyzeLog(String step, ForecastOrderLineSDO orderLine, String result) {
        log.info(PREFIX_LOG_BIZ + step + "| 批次号: " + getBatchNo() + "| 子单id: | " + orderLine + " | " + lastMessage + " | 结果: " + result);
    }

    /**
     * 库存分解检查阶段异常错误
     *
     * @param msg
     * @param orderLine
     * @param e
     */
    public void sysErrorLogInDistributeInventory(String msg, ForecastOrderLineSDO orderLine, Throwable e) {
        sysErrorLog("库存分解", msg, orderLine, e);
    }

    /**
     * 系统日志记录
     *
     * @param msg
     */
    public void sysErrorLog(String step, String msg, ForecastOrderLineSDO orderLine, Throwable e) {
        if (Objects.isNull(orderLine)) {
            log.error(PREFIX_LOG_SYS_ERROR + " | Step:" + step + " | 批次号: " + getBatchNo() + "| 结果: " + msg + "| Exception: " + e.getMessage(), e);

        } else {
            log.error(PREFIX_LOG_SYS_ERROR + "| Step:" + step + " | 批次号: " + getBatchNo() + "| 子单ID: " + orderLine.getId() + "| 结果: " + msg + "| Exception: " + e.getMessage(), e);
        }
    }
}
